package com.yxinsur.imc.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcInvoice;
import com.yxinsur.imc.business.form.ImcInvoiceForm;
import com.yxinsur.imc.business.mapper.ImcInvoiceMapper;
import com.yxinsur.imc.business.service.IImcInvoiceService;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 发票Service业务层处理
 *
 * @author imc
 * @date 2021-05-31
 */
@Service
public class ImcInvoiceServiceImpl extends ServiceImpl<ImcInvoiceMapper, ImcInvoice> implements IImcInvoiceService {
    @Autowired
    private ImcInvoiceMapper imcInvoiceMapper;

    /**
     * 查询发票
     *
     * @param id 发票ID
     * @return 发票
     */
    @Override
    public ImcInvoice selectImcInvoiceById(String id) {
        return imcInvoiceMapper.selectImcInvoiceById(id);
    }

    /**
     * 查询发票列表
     *
     * @param imcInvoiceForm 发票
     * @return 发票
     */
    @Override
    public List<ImcInvoice> selectImcInvoiceList(ImcInvoiceForm imcInvoiceForm) {
        ImcInvoice imcInvoice = new ImcInvoice();
        BeanUtil.copyProperties(imcInvoiceForm, imcInvoice);
        imcInvoice.setTenantId(SecurityUtils.getTenantId());
        return imcInvoiceMapper.selectImcInvoiceList(imcInvoice);
    }

    /**
     * 新增发票
     *
     * @param imcInvoiceForm 发票
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertImcInvoice(ImcInvoiceForm imcInvoiceForm) {
        ImcInvoice imcInvoice = new ImcInvoice();
        BeanUtil.copyProperties(imcInvoiceForm, imcInvoice);
        imcInvoice.setInvoiceUrl(JSONUtil.toJsonStr(imcInvoiceForm.getInvoiceUrlList()));
        imcInvoice.setInvoiceStatus("A");
        imcInvoice.setPrem(new BigDecimal("0.0"));
        imcInvoice.setTenantId(SecurityUtils.getTenantId());
        imcInvoice.setCreateId(SecurityUtils.getUsername());
        imcInvoice.setCreateTime(DateUtils.getNowDate());
        return imcInvoiceMapper.insertImcInvoice(imcInvoice);
    }

    /**
     * 修改发票
     *
     * @param imcInvoice 发票
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateImcInvoice(ImcInvoice imcInvoice) {
        imcInvoice.setUpdateTime(DateUtils.getNowDate());
        return imcInvoiceMapper.updateImcInvoice(imcInvoice);
    }

    /**
     * 批量删除发票
     *
     * @param ids 需要删除的发票ID
     * @return 结果
     */
    @Override
    public int deleteImcInvoiceByIds(String[] ids) {
        return imcInvoiceMapper.deleteImcInvoiceByIds(ids);
    }

    /**
     * 删除发票信息
     *
     * @param id 发票ID
     * @return 结果
     */
    @Override
    public int deleteImcInvoiceById(String id) {
        return imcInvoiceMapper.deleteImcInvoiceById(id);
    }
}
